package com.vivien.example.springbootdemo.controller;

import com.vivien.example.springbootdemo.pojo.User;
import com.vivien.example.springbootdemo.util.JwtUtil;
import com.vivien.example.springbootdemo.vo.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Tag(name = "用户服务",description = "用户信息的CRUD")
//@RequestMapping(value = "user")
public class UserController {

    @PostMapping("/login")
    public Result login(String username,String password){
        //service  findUser
        //User user=
//        if(user==null){
//            return new Result(500001,"用户不存在",null);
//        }

        Map<String,Object> claims=new HashMap<>();
        claims.put("id",1);
        claims.put("username","vivien");

        String token= JwtUtil.genToken(claims);

        return new Result(0000,"成功",token);
    }

//    @Autowired
//    private GoodstypeService service;

//    @RequestMapping(value = "user",method = RequestMethod.GET)
    @GetMapping(value = "user")
    @Operation(summary = "获取列表",description = "获取所有用户信息的列表数据")
    public List<User> getAllUser(){

//        System.out.println(service.list());


        List list=new ArrayList<User>();

        User user1=new User();
        user1.setId(0);
        user1.setName("vivien");
        user1.setPassword("123456");

        User user2=new User(1,"tom","ssdf");

        list.add(user1);
        list.add(user2);

        return list;
    }

    //get  xxxx/user/0001
    @GetMapping("user/{id}")
    @Operation(summary = "查询用户",description = "查询某个用户的详情信息")
    public User getOne(
            @Parameter(in = ParameterIn.PATH,description = "ID",required = true)
            @PathVariable(name = "id") int id
    ){
        System.out.println(id);

        User user=new User();
        user.setId(0);
        user.setName("vivien");
        user.setPassword("12344");

        return user;
    }

    @PostMapping("user")
    public int addOne(@RequestBody User user){
        System.out.println("add");
        System.out.println(user);
        return 0;
    }

    @PutMapping("user")
    public int updateUser(@RequestBody User user){
        System.out.println("update");
        System.out.println(user);
        return 0;
    }

    @DeleteMapping("user/{id}")
    public int delUser(@PathVariable("id") int id){
        System.out.println("delete");
        System.out.println(id);
        return 0;
    }
}
